package com.jin.webvideo.util.base;

import com.alibaba.fastjson.JSONObject;
import com.jin.webvideo.util.entity.EntityUtil;
import java.util.HashMap;
import java.util.Map;

public class MapUtil {
    public static Map<String,String> toMap(Object obj, boolean skipTransientField){
        Map<String, String> result = new HashMap<>();
        String json = EntityUtil.jsonSerializer(obj, skipTransientField);
        JSONObject object = JSONObject.parseObject(json);
        for (String key : object.keySet()){
            result.put(key, object.getString(key));
        }
        return result;
    }
    public static Map<String,String> toMap(Object obj){
        return toMap(obj, true);
    }

    public static<T, V> Map<T, V> copyByKeys(Map<T, V> map, V defaultVal, T... keys) {
        Map<T, V> r = new HashMap<>();
        for (T key : keys) {
            V v = map.get(key);
            if (v == null) {
                v = defaultVal;
            }
            r.put(key, v);
        }
        return r;
    }
}
